package com.atguigu.service.impl;

import com.atguigu.service.CouponServiceV2;
import com.atguigu.utils.TaskBatchSendUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: shenhao
 * @CreatTime: 2024/10/27 12:54
 * @Version: 1.0
 */
@Service
public class CouponServiceImplV2 implements CouponServiceV2 {
    public static final Integer  COUPON_NUMBER=50;
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @SneakyThrows
    @Override
    public void batchTaskActionV2() {
        List<String> coupons = getCoupons();
        long startTime = System.currentTimeMillis();
        //2 调用工具类批处理任务,这些优惠卷coupons，放入线程池threadPool，做什么业务disposeTask下发
        TaskBatchSendUtils.send(coupons,threadPoolTaskExecutor,TaskBatchSendUtils::disposeTaskV2);
        long endTime = System.currentTimeMillis();
        System.out.println("---cost time: " + (endTime - startTime)+"ms");
    }

    private static List<String> getCoupons(){
        List<String> coupons=new ArrayList<>();
        for(int i=1;i<=COUPON_NUMBER;i++){
            coupons.add("优惠卷--"+i);
        }
        return coupons;
    }
}
